<?php

namespace HyperfTest\Cases\Service;

use App\Exception\BusinessException;
use App\Model\UserModel;
use App\Utils\SmsUtil;
use Hyperf\Testing\TestCase;
use Hyperf\Di\Annotation\Inject;
use App\Service\UserService;

class UserServiceTest extends TestCase
{
    /**
     * @test
     */
    public function register(){
        $account = "bruce";
        $password = "123456";
        try {
            $user = $this->getContainer()->get(UserService::class)->registerByAccount($account, $password);
            $this->assertInstanceOf(UserModel::class, $user, "用户注册失败");
        }catch (BusinessException $e){
            $this->assertNotEmpty($e, $e->getMessage());
        }
    }

    /**
     * @test
     * @group sms
     */
    public function sendSms(){
        $mobile = "1391234". mt_rand(1000, 9999);
        $code = $this->getContainer()->get(UserService::class)->sendSms($mobile);
        $this->assertIsInt($code, "短信发送失败");
        return [$mobile, $code];
    }


    /**
     * 测试主流程的分支
     * @test
     * @group register-mobile
     * @group register
     * @depends sendSms
     */
    public function registerMobile1($agrs){
        var_dump($agrs);
        $mobile = $agrs[0];
        $smsCode = $agrs[1];
        try {
            $user = $this->getContainer()->get(UserService::class)->registerByMobile($mobile, $smsCode);
            $this->assertInstanceOf(UserModel::class, $user, "注册失败");
        }catch (BusinessException $e){
            var_dump($e->getMessage());
        }
    }

    /**
     * 测试手机没有发短信的情况
     * @test
     * @group register-mobile-notsms
     * @group register
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function registerMobile2(){
        $mobile = "13812345678";
        try {
            $user = $this->getContainer()->get(UserService::class)->registerByMobile($mobile, '1234');
            $this->assertInstanceOf(UserModel::class, $user, "注册失败");
        }catch (BusinessException $e){
            var_dump($e->getMessage());
        }
    }

}